<?php

// +----------------------------------------------------------------------
// | Copyright (c) 2020 All rights reserved.
// +----------------------------------------------------------------------
// | User: Simony <1362229026@qq.com>
// | wechat: Shen86579595
// +----------------------------------------------------------------------
namespace app\admin\controller;

use easemob\EasemobIm;
use think\Db;
use JPush\Client as JPush;

class Message extends Common
{
    /**
     * User:Simony
     * Function: 消息表列表
     * @return mixed
     */
    public function systemList()
    {
        //params
        $field = input('get.field', '');
        $value = input('get.value', '');
        $search_field = input('get.search_field', '');
        $search_value = input('get.search_value', '');

        //where
        $where = [];

        //judge
        if ($field) $where[$field] = $value;
        if ($search_field) $where[$search_field] = $search_value;

        //model
        $messageModel = model('common/message/Message');

        $where = [
            'member_id' => -1,
            'type' => 0,
        ];
        //getData
        $list = $messageModel->getList($where, 10);


        //assign
        $this->assign('list', $list);
        $this->assign('page', $messageModel->page_info->render());
        return $this->fetch();
    }


    /**
     * User:Simony
     * Function: 消息表添加修改
     */
    public function systemAdd()
    {
        //params
        $id = input('param.id/d', '');

        //model
        $messageModel = model('common/message/Message');
        $data = $messageModel->getInfoByID($id);

        if ($this->request->isAjax()) {

            //post params
            $ajax_data = $this->request->post();
            $ajax_data['create_time'] = time();
            $choose = $ajax_data['choose'];
            unset($ajax_data['choose']);

            if ($ajax_data['receive_member_id'] != -1) {
                $member = model('common/member/Member')->getInfoByID($ajax_data['receive_member_id']);
                if (empty($member)) ds_json_encode('err', '接收消息会员不存在');
            }

            //judge
            if ($id && !$data) ds_json_encode('err', '数据不存在');

            $ajax_data['content'] = htmlspecialchars($ajax_data['content']);
            Db::startTrans();
            $res = $id ? $messageModel->edit(['id' => $id], $ajax_data) : $messageModel->add($ajax_data);
            if (!$res) {
                Db::rollback();
                ds_json_encode('err', '操作失败');
            }

            $noticleLogic = model('common/Noticle', 'logic');
            if ($choose == 1) {
                $groupids = model('common/easemoblm/EasemoblmGroup')->getCol('groupid');
                foreach ($groupids as $groupid) {
                    $res = $noticleLogic->sendTxtSysNotice($groupid, $ajax_data['content'], 7, 'chatgroups');
                }
            } else {
                $res = $noticleLogic->sendTxtSysNotice($ajax_data['receive_member_id'], $ajax_data['content'], 7);
            }


            Db::commit();
            ds_json_encode('ret', '操作成功');
        }
        $this->assign('data', $data);
        return $this->fetch();
    }

}